<?php
 class Articles extends CI_Model{
    function __construct(){
        parent::__construct();
    }
    
     /** 
     * Insert data for config
     * @author tien.nguyen
     */
    public function getList(){ 
        $this->db->select();
        $this->db->from('news');
        $this->db->join('news_categories', 'news_categories.NewsCategoryId = 
                news.CategoryId');
        $this->db->where('news.Visible', 1);
        $this->db->order_by('news.CreatedDate DESC');
        $query = $this->db->get();
        $res = $query->result_array();
        return $res;
    }
    
        
    /**
     * Get product by product id
     * @author tien.nguyen
     */
    public function getArticle($articleId){
        $query = $this->db->get_where('news', array('NewsId' => $articleId));
        return $query->row_array();
    }
    
    /**
     * edit function in config
     * @author tien.nguyen
     */
    public function edit($data, $articleId){
        $data = $this->_convertData($data);
        return $this->db->update('news', $data, array('NewsId' => $articleId));
    }
    
    /**
     * Add customer
     * @author tien.nguyen 
     */
    public function add($data){
        $data['CreatedDate'] = date('Y-m-d H:m:s');
        return $this->db->insert('news', $data);
    }
    
    /**
     * Delete customer
     * @author tien.nguyen
     */
    public function delete($articleId){
        return $this->db->delete('news', array('NewsId' => $articleId));
    }
    
    /**
     * Convert data array to array of product
     * @author tien.nguyen
     */
    private function _convertData($data){
        $res = array();
        $arr = array('NewsId', 'NewsTitle', 'NewsShort', 
            'NewsDescription', 'Visible', 'CreatedDate', 'Image',
            'CategoryId');
        foreach($arr as $item){
            if(isset($data[$item])){
                $res[$item] = $data[$item];
            }
        }
        
        //set default value
        $res['Visible'] = isset($res['Visible']) ? $res['Visible'] : 0;
        return $res;
    }
 }
?>